package net.lwf.handler.wp.helper;

import java.util.Map;

import lwf.factory.PropertyReader;
import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.number.util.RandomNumberGenerator;
import net.lwf.string.util.StringManipulator;

public class WpImpl {
	
	protected PropertyReader propertyreader = PropertyReader.getInstance();

    public String replacePH(String sSource, String phname) {
    	
    	Map mapSource = this.getPhData(phname);
    	String newString = sSource;
    	String replace = null;
    	String find = "[" + phname + "_";
        //Check
    	if(sSource.indexOf(find) == -1) {
    		return sSource;
    	}
    	
    	int quantity = StringManipulator.countOccurrences(sSource, find);

    	if(quantity > 1) {
        	String[] srandom = RandomNumberGenerator.generate(1, mapSource.size(), quantity);
        	for(int i=0; i<srandom.length; i++) {
        		find = "[" + phname + "_" + String.valueOf(i+1) + "]";
        		if(newString.indexOf(find) != -1) {
            		replace = (String) mapSource.get(srandom[i]);
            		newString = StringManipulator.replace(newString, find, replace);
        		}
        	}    		
    	}
    	else {
    		find = "[" + phname + "_1]";
    		String srand = String.valueOf(RandomNumberGenerator.generate(1, mapSource.size()));
    		replace = (String) mapSource.get(srand);
    		newString = StringManipulator.replace(newString, find, replace);
    	}

    	return newString;
    }
	
	
    public String replacePH(String sSource, String phname, int quantity) {
    	
    	Map mapSource = this.getPhData(phname);
    	String newString = sSource;
    	String replace = null;
    	String find = "[" + phname + "_";
        //Check
    	if(sSource.indexOf(find) == -1) {
    		return sSource;
    	}

    	if(quantity > 1) {
        	String[] srandom = RandomNumberGenerator.generate(1, mapSource.size(), quantity);
        	for(int i=0; i<srandom.length; i++) {
        		find = "[" + phname + "_" + String.valueOf(i+1) + "]";
        		replace = (String) mapSource.get(srandom[i]);
        		newString = StringManipulator.replace(newString, find, replace);
        	}    		
    	}
    	else {
    		find = "[" + phname + "_1]";
    		String srand = String.valueOf(RandomNumberGenerator.generate(1, mapSource.size()));
    		replace = (String) mapSource.get(srand);
    		newString = StringManipulator.replace(newString, find, replace);
    	}

    	return newString;
    }
    
    public String replaceValues(String sSource, String[] values) {
    	String newString = sSource;
    	for(int i=0; i<values.length; i++) {
    		String find = "[VALUE_" + String.valueOf(i+1) + "]";
    		String replace = values[i];
    		newString = StringManipulator.replace(newString, find, replace);
    	}
    	return newString;
    }
    
    private Map getPhData(String phname) {
    	if (phname.equals(IHandlerConstant.DAY)) {
    		return propertyreader.getDays();
    	} 
    	else if (phname.equals(IHandlerConstant.ANIMAL)) {
    		return propertyreader.getAnimals();
    	}
    	else if (phname.equals(IHandlerConstant.ITEM)) {
    		return propertyreader.getItems();
    	}    
    	else if (phname.equals(IHandlerConstant.LIQUID)) {
    		return propertyreader.getLiquids();
    	}    
    	else if (phname.equals(IHandlerConstant.NAME)) {
    		return propertyreader.getNames();
    	}
    	else if (phname.equals(IHandlerConstant.COLOR)) {
    		return propertyreader.getColors();
    	}
    	else if (phname.equals(IHandlerConstant.FRUITS)) {
    		return propertyreader.getFruits();
    	}
    	return null;
    }


}
